{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "import pandas as pd\n",
    "loans_2007 = pd.read_csv('LoanStats3a.csv', skiprows=1)\n",
    "half_count = len(loans_2007) / 2\n",
    "loans_2007 = loans_2007.dropna(thresh=half_count, axis=1)\n",
    "loans_2007 = loans_2007.drop(['desc', 'url'],axis=1)\n",
    "loans_2007.to_csv('loans_2007.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id                                1077501\n",
      "member_id                      1.2966e+06\n",
      "loan_amnt                            5000\n",
      "funded_amnt                          5000\n",
      "funded_amnt_inv                      4975\n",
      "term                            36 months\n",
      "int_rate                           10.65%\n",
      "installment                        162.87\n",
      "grade                                   B\n",
      "sub_grade                              B2\n",
      "emp_title                             NaN\n",
      "emp_length                      10+ years\n",
      "home_ownership                       RENT\n",
      "annual_inc                          24000\n",
      "verification_status              Verified\n",
      "issue_d                          Dec-2011\n",
      "loan_status                    Fully Paid\n",
      "pymnt_plan                              n\n",
      "purpose                       credit_card\n",
      "title                            Computer\n",
      "zip_code                            860xx\n",
      "addr_state                             AZ\n",
      "dti                                 27.65\n",
      "delinq_2yrs                             0\n",
      "earliest_cr_line                 Jan-1985\n",
      "inq_last_6mths                          1\n",
      "open_acc                                3\n",
      "pub_rec                                 0\n",
      "revol_bal                           13648\n",
      "revol_util                          83.7%\n",
      "total_acc                               9\n",
      "initial_list_status                     f\n",
      "out_prncp                               0\n",
      "out_prncp_inv                           0\n",
      "total_pymnt                       5863.16\n",
      "total_pymnt_inv                   5833.84\n",
      "total_rec_prncp                      5000\n",
      "total_rec_int                      863.16\n",
      "total_rec_late_fee                      0\n",
      "recoveries                              0\n",
      "collection_recovery_fee                 0\n",
      "last_pymnt_d                     Jan-2015\n",
      "last_pymnt_amnt                    171.62\n",
      "last_credit_pull_d               Nov-2016\n",
      "collections_12_mths_ex_med              0\n",
      "policy_code                             1\n",
      "application_type               INDIVIDUAL\n",
      "acc_now_delinq                          0\n",
      "chargeoff_within_12_mths                0\n",
      "delinq_amnt                             0\n",
      "pub_rec_bankruptcies                    0\n",
      "tax_liens                               0\n",
      "Name: 0, dtype: object\n",
      "52\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "loans_2007 = pd.read_csv(\"loans_2007.csv\")\n",
    "loans_2007.drop_duplicates()\n",
    "print(loans_2007.iloc[0])\n",
    "print(loans_2007.shape[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "loans_2007 = loans_2007.drop([\"id\", \"member_id\", \"funded_amnt\", \"funded_amnt_inv\", \"grade\", \"sub_grade\", \"emp_title\", \"issue_d\"], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "loans_2007 = loans_2007.drop([\"zip_code\", \"out_prncp\", \"out_prncp_inv\", \"total_pymnt\", \"total_pymnt_inv\", \"total_rec_prncp\"], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loan_amnt                            5000\n",
      "term                            36 months\n",
      "int_rate                           10.65%\n",
      "installment                        162.87\n",
      "emp_length                      10+ years\n",
      "home_ownership                       RENT\n",
      "annual_inc                          24000\n",
      "verification_status              Verified\n",
      "loan_status                    Fully Paid\n",
      "pymnt_plan                              n\n",
      "purpose                       credit_card\n",
      "title                            Computer\n",
      "addr_state                             AZ\n",
      "dti                                 27.65\n",
      "delinq_2yrs                             0\n",
      "earliest_cr_line                 Jan-1985\n",
      "inq_last_6mths                          1\n",
      "open_acc                                3\n",
      "pub_rec                                 0\n",
      "revol_bal                           13648\n",
      "revol_util                          83.7%\n",
      "total_acc                               9\n",
      "initial_list_status                     f\n",
      "last_credit_pull_d               Nov-2016\n",
      "collections_12_mths_ex_med              0\n",
      "policy_code                             1\n",
      "application_type               INDIVIDUAL\n",
      "acc_now_delinq                          0\n",
      "chargeoff_within_12_mths                0\n",
      "delinq_amnt                             0\n",
      "pub_rec_bankruptcies                    0\n",
      "tax_liens                               0\n",
      "Name: 0, dtype: object\n",
      "32\n"
     ]
    }
   ],
   "source": [
    "\n",
    "loans_2007 = loans_2007.drop([\"total_rec_int\", \"total_rec_late_fee\", \"recoveries\", \"collection_recovery_fee\", \"last_pymnt_d\", \"last_pymnt_amnt\"], axis=1)\n",
    "print(loans_2007.iloc[0])\n",
    "print(loans_2007.shape[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fully Paid                                             33902\n",
      "Charged Off                                             5658\n",
      "Does not meet the credit policy. Status:Fully Paid      1988\n",
      "Does not meet the credit policy. Status:Charged Off      761\n",
      "Current                                                  201\n",
      "Late (31-120 days)                                        10\n",
      "In Grace Period                                            9\n",
      "Late (16-30 days)                                          5\n",
      "Default                                                    1\n",
      "Name: loan_status, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "\n",
    "print(loans_2007['loan_status'].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "loans_2007 = loans_2007[(loans_2007['loan_status'] == \"Fully Paid\") | (loans_2007['loan_status'] == \"Charged Off\")]\n",
    "\n",
    "status_replace = {\n",
    "    \"loan_status\" : {\n",
    "        \"Fully Paid\": 1,\n",
    "        \"Charged Off\": 0,\n",
    "    }\n",
    "}\n",
    "\n",
    "loans_2007 = loans_2007.replace(status_replace)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['initial_list_status', 'collections_12_mths_ex_med', 'policy_code', 'application_type', 'acc_now_delinq', 'chargeoff_within_12_mths', 'delinq_amnt', 'tax_liens']\n",
      "(39560, 24)\n"
     ]
    }
   ],
   "source": [
    "#let's look for any columns that contain only one unique value and remove them\n",
    "\n",
    "orig_columns = loans_2007.columns\n",
    "drop_columns = []\n",
    "for col in orig_columns:\n",
    "    col_series = loans_2007[col].dropna().unique()\n",
    "    if len(col_series) == 1:\n",
    "        drop_columns.append(col)\n",
    "loans_2007 = loans_2007.drop(drop_columns, axis=1)\n",
    "print(drop_columns)\n",
    "print loans_2007.shape\n",
    "loans_2007.to_csv('filtered_loans_2007.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loan_amnt                 0\n",
      "term                      0\n",
      "int_rate                  0\n",
      "installment               0\n",
      "emp_length                0\n",
      "home_ownership            0\n",
      "annual_inc                0\n",
      "verification_status       0\n",
      "loan_status               0\n",
      "pymnt_plan                0\n",
      "purpose                   0\n",
      "title                    10\n",
      "addr_state                0\n",
      "dti                       0\n",
      "delinq_2yrs               0\n",
      "earliest_cr_line          0\n",
      "inq_last_6mths            0\n",
      "open_acc                  0\n",
      "pub_rec                   0\n",
      "revol_bal                 0\n",
      "revol_util               50\n",
      "total_acc                 0\n",
      "last_credit_pull_d        2\n",
      "pub_rec_bankruptcies    697\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "loans = pd.read_csv('filtered_loans_2007.csv')\n",
    "null_counts = loans.isnull().sum()\n",
    "print(null_counts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "object     12\n",
      "float64    10\n",
      "int64       1\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "\n",
    "loans = loans.drop(\"pub_rec_bankruptcies\", axis=1)\n",
    "loans = loans.dropna(axis=0)\n",
    "print(loans.dtypes.value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "term                     36 months\n",
      "int_rate                    10.65%\n",
      "emp_length               10+ years\n",
      "home_ownership                RENT\n",
      "verification_status       Verified\n",
      "pymnt_plan                       n\n",
      "purpose                credit_card\n",
      "title                     Computer\n",
      "addr_state                      AZ\n",
      "earliest_cr_line          Jan-1985\n",
      "revol_util                   83.7%\n",
      "last_credit_pull_d        Nov-2016\n",
      "Name: 0, dtype: object\n"
     ]
    }
   ],
   "source": [
    "object_columns_df = loans.select_dtypes(include=[\"object\"])\n",
    "print(object_columns_df.iloc[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RENT        18780\n",
      "MORTGAGE    17574\n",
      "OWN          3045\n",
      "OTHER          96\n",
      "NONE            3\n",
      "Name: home_ownership, dtype: int64\n",
      "Not Verified       16856\n",
      "Verified           12705\n",
      "Source Verified     9937\n",
      "Name: verification_status, dtype: int64\n",
      "10+ years    8821\n",
      "< 1 year     4563\n",
      "2 years      4371\n",
      "3 years      4074\n",
      "4 years      3409\n",
      "5 years      3270\n",
      "1 year       3227\n",
      "6 years      2212\n",
      "7 years      1756\n",
      "8 years      1472\n",
      "9 years      1254\n",
      "n/a          1069\n",
      "Name: emp_length, dtype: int64\n",
      " 36 months    29041\n",
      " 60 months    10457\n",
      "Name: term, dtype: int64\n",
      "CA    7070\n",
      "NY    3788\n",
      "FL    2856\n",
      "TX    2714\n",
      "NJ    1838\n",
      "IL    1517\n",
      "PA    1504\n",
      "VA    1400\n",
      "GA    1393\n",
      "MA    1336\n",
      "OH    1208\n",
      "MD    1049\n",
      "AZ     874\n",
      "WA     834\n",
      "CO     786\n",
      "NC     780\n",
      "CT     747\n",
      "MI     722\n",
      "MO     682\n",
      "MN     611\n",
      "NV     492\n",
      "SC     470\n",
      "WI     453\n",
      "AL     446\n",
      "OR     445\n",
      "LA     435\n",
      "KY     325\n",
      "OK     298\n",
      "KS     269\n",
      "UT     256\n",
      "AR     243\n",
      "DC     211\n",
      "RI     198\n",
      "NM     188\n",
      "WV     176\n",
      "HI     172\n",
      "NH     172\n",
      "DE     113\n",
      "MT      84\n",
      "WY      83\n",
      "AK      79\n",
      "SD      63\n",
      "VT      54\n",
      "MS      19\n",
      "TN      17\n",
      "IN       9\n",
      "ID       6\n",
      "IA       5\n",
      "NE       5\n",
      "ME       3\n",
      "Name: addr_state, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "cols = ['home_ownership', 'verification_status', 'emp_length', 'term', 'addr_state']\n",
    "for c in cols:\n",
    "    print(loans[c].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debt_consolidation    18533\n",
      "credit_card            5099\n",
      "other                  3963\n",
      "home_improvement       2965\n",
      "major_purchase         2181\n",
      "small_business         1815\n",
      "car                    1544\n",
      "wedding                 945\n",
      "medical                 692\n",
      "moving                  581\n",
      "vacation                379\n",
      "house                   378\n",
      "educational             320\n",
      "renewable_energy        103\n",
      "Name: purpose, dtype: int64\n",
      "Debt Consolidation                         2168\n",
      "Debt Consolidation Loan                    1706\n",
      "Personal Loan                               658\n",
      "Consolidation                               509\n",
      "debt consolidation                          502\n",
      "Credit Card Consolidation                   356\n",
      "Home Improvement                            354\n",
      "Debt consolidation                          333\n",
      "Small Business Loan                         322\n",
      "Credit Card Loan                            313\n",
      "Personal                                    308\n",
      "Consolidation Loan                          255\n",
      "Home Improvement Loan                       246\n",
      "personal loan                               234\n",
      "personal                                    220\n",
      "Loan                                        212\n",
      "Wedding Loan                                209\n",
      "consolidation                               200\n",
      "Car Loan                                    200\n",
      "Other Loan                                  190\n",
      "Credit Card Payoff                          155\n",
      "Wedding                                     152\n",
      "Major Purchase Loan                         144\n",
      "Credit Card Refinance                       143\n",
      "Consolidate                                 127\n",
      "Medical                                     122\n",
      "Credit Card                                 117\n",
      "home improvement                            111\n",
      "My Loan                                      94\n",
      "Credit Cards                                 93\n",
      "                                           ... \n",
      "DebtConsolidationn                            1\n",
      " Freedom                                      1\n",
      "Credit Card Consolidation Loan - SEG          1\n",
      "SOLAR PV                                      1\n",
      "Pay on Credit card                            1\n",
      "To pay off balloon payments due               1\n",
      "Paying off the debt                           1\n",
      "Payoff ING PLOC                               1\n",
      "Josh CC Loan                                  1\n",
      "House payoff                                  1\n",
      "Taking care of Business                       1\n",
      "Gluten Free Bakery in ideal town for it       1\n",
      "Startup Money for Small Business              1\n",
      "FundToFinanceCar                              1\n",
      "getting ready for Baby                        1\n",
      "Dougs Wedding Loan                            1\n",
      "d rock                                        1\n",
      "LC Loan 2                                     1\n",
      "swimming pool repair                          1\n",
      "engagement                                    1\n",
      "Cut the credit cards Loan                     1\n",
      "vinman                                        1\n",
      "working hard to get out of debt               1\n",
      "consolidate the rest of my debt               1\n",
      "Medical/Vacation                              1\n",
      "2BDebtFree                                    1\n",
      "Paying Off High Interest Credit Cards!        1\n",
      "Baby on the way!                              1\n",
      "cart loan                                     1\n",
      "Consolidaton                                  1\n",
      "Name: title, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "\n",
    "print(loans[\"purpose\"].value_counts())\n",
    "print(loans[\"title\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "mapping_dict = {\n",
    "    \"emp_length\": {\n",
    "        \"10+ years\": 10,\n",
    "        \"9 years\": 9,\n",
    "        \"8 years\": 8,\n",
    "        \"7 years\": 7,\n",
    "        \"6 years\": 6,\n",
    "        \"5 years\": 5,\n",
    "        \"4 years\": 4,\n",
    "        \"3 years\": 3,\n",
    "        \"2 years\": 2,\n",
    "        \"1 year\": 1,\n",
    "        \"< 1 year\": 0,\n",
    "        \"n/a\": 0\n",
    "    }\n",
    "}\n",
    "loans = loans.drop([\"last_credit_pull_d\", \"earliest_cr_line\", \"addr_state\", \"title\"], axis=1)\n",
    "loans[\"int_rate\"] = loans[\"int_rate\"].str.rstrip(\"%\").astype(\"float\")\n",
    "loans[\"revol_util\"] = loans[\"revol_util\"].str.rstrip(\"%\").astype(\"float\")\n",
    "loans = loans.replace(mapping_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "cat_columns = [\"home_ownership\", \"verification_status\", \"emp_length\", \"purpose\", \"term\"]\n",
    "dummy_df = pd.get_dummies(loans[cat_columns])\n",
    "loans = pd.concat([loans, dummy_df], axis=1)\n",
    "loans = loans.drop(cat_columns, axis=1)\n",
    "loans = loans.drop(\"pymnt_plan\", axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "loans.to_csv('cleaned_loans2007.csv', index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 39498 entries, 0 to 39497\n",
      "Data columns (total 37 columns):\n",
      "loan_amnt                              39498 non-null float64\n",
      "int_rate                               39498 non-null float64\n",
      "installment                            39498 non-null float64\n",
      "annual_inc                             39498 non-null float64\n",
      "loan_status                            39498 non-null int64\n",
      "dti                                    39498 non-null float64\n",
      "delinq_2yrs                            39498 non-null float64\n",
      "inq_last_6mths                         39498 non-null float64\n",
      "open_acc                               39498 non-null float64\n",
      "pub_rec                                39498 non-null float64\n",
      "revol_bal                              39498 non-null float64\n",
      "revol_util                             39498 non-null float64\n",
      "total_acc                              39498 non-null float64\n",
      "home_ownership_MORTGAGE                39498 non-null int64\n",
      "home_ownership_NONE                    39498 non-null int64\n",
      "home_ownership_OTHER                   39498 non-null int64\n",
      "home_ownership_OWN                     39498 non-null int64\n",
      "home_ownership_RENT                    39498 non-null int64\n",
      "verification_status_Not Verified       39498 non-null int64\n",
      "verification_status_Source Verified    39498 non-null int64\n",
      "verification_status_Verified           39498 non-null int64\n",
      "purpose_car                            39498 non-null int64\n",
      "purpose_credit_card                    39498 non-null int64\n",
      "purpose_debt_consolidation             39498 non-null int64\n",
      "purpose_educational                    39498 non-null int64\n",
      "purpose_home_improvement               39498 non-null int64\n",
      "purpose_house                          39498 non-null int64\n",
      "purpose_major_purchase                 39498 non-null int64\n",
      "purpose_medical                        39498 non-null int64\n",
      "purpose_moving                         39498 non-null int64\n",
      "purpose_other                          39498 non-null int64\n",
      "purpose_renewable_energy               39498 non-null int64\n",
      "purpose_small_business                 39498 non-null int64\n",
      "purpose_vacation                       39498 non-null int64\n",
      "purpose_wedding                        39498 non-null int64\n",
      "term_ 36 months                        39498 non-null int64\n",
      "term_ 60 months                        39498 non-null int64\n",
      "dtypes: float64(12), int64(25)\n",
      "memory usage: 11.1 MB\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "loans = pd.read_csv(\"cleaned_loans2007.csv\")\n",
    "print(loans.info())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "# False positives.\n",
    "fp_filter = (predictions == 1) & (loans[\"loan_status\"] == 0)\n",
    "fp = len(predictions[fp_filter])\n",
    "\n",
    "# True positives.\n",
    "tp_filter = (predictions == 1) & (loans[\"loan_status\"] == 1)\n",
    "tp = len(predictions[tp_filter])\n",
    "\n",
    "# False negatives.\n",
    "fn_filter = (predictions == 0) & (loans[\"loan_status\"] == 1)\n",
    "fn = len(predictions[fn_filter])\n",
    "\n",
    "# True negatives\n",
    "tn_filter = (predictions == 0) & (loans[\"loan_status\"] == 0)\n",
    "tn = len(predictions[tn_filter])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "lr = LogisticRegression()\n",
    "cols = loans.columns\n",
    "train_cols = cols.drop(\"loan_status\")\n",
    "features = loans[train_cols]\n",
    "target = loans[\"loan_status\"]\n",
    "lr.fit(features, target)\n",
    "predictions = lr.predict(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.999084438406\n",
      "0.998049299521\n",
      "0     1\n",
      "1     1\n",
      "2     1\n",
      "3     1\n",
      "4     1\n",
      "5     1\n",
      "6     1\n",
      "7     1\n",
      "8     1\n",
      "9     1\n",
      "10    1\n",
      "11    1\n",
      "12    1\n",
      "13    1\n",
      "14    1\n",
      "15    1\n",
      "16    1\n",
      "17    1\n",
      "18    1\n",
      "19    1\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.cross_validation import cross_val_predict, KFold\n",
    "lr = LogisticRegression()\n",
    "kf = KFold(features.shape[0], random_state=1)\n",
    "predictions = cross_val_predict(lr, features, target, cv=kf)\n",
    "predictions = pd.Series(predictions)\n",
    "\n",
    "# False positives.\n",
    "fp_filter = (predictions == 1) & (loans[\"loan_status\"] == 0)\n",
    "fp = len(predictions[fp_filter])\n",
    "\n",
    "# True positives.\n",
    "tp_filter = (predictions == 1) & (loans[\"loan_status\"] == 1)\n",
    "tp = len(predictions[tp_filter])\n",
    "\n",
    "# False negatives.\n",
    "fn_filter = (predictions == 0) & (loans[\"loan_status\"] == 1)\n",
    "fn = len(predictions[fn_filter])\n",
    "\n",
    "# True negatives\n",
    "tn_filter = (predictions == 0) & (loans[\"loan_status\"] == 0)\n",
    "tn = len(predictions[tn_filter])\n",
    "\n",
    "# Rates\n",
    "tpr = tp / float((tp + fn))\n",
    "fpr = fp / float((fp + tn))\n",
    "\n",
    "print(tpr)\n",
    "print(fpr)\n",
    "print predictions[:20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.670781771464\n",
      "0.400780280192\n",
      "0     1\n",
      "1     0\n",
      "2     0\n",
      "3     1\n",
      "4     1\n",
      "5     0\n",
      "6     0\n",
      "7     0\n",
      "8     0\n",
      "9     0\n",
      "10    1\n",
      "11    0\n",
      "12    1\n",
      "13    1\n",
      "14    0\n",
      "15    0\n",
      "16    1\n",
      "17    1\n",
      "18    1\n",
      "19    0\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.cross_validation import cross_val_predict\n",
    "lr = LogisticRegression(class_weight=\"balanced\")\n",
    "kf = KFold(features.shape[0], random_state=1)\n",
    "predictions = cross_val_predict(lr, features, target, cv=kf)\n",
    "predictions = pd.Series(predictions)\n",
    "\n",
    "# False positives.\n",
    "fp_filter = (predictions == 1) & (loans[\"loan_status\"] == 0)\n",
    "fp = len(predictions[fp_filter])\n",
    "\n",
    "# True positives.\n",
    "tp_filter = (predictions == 1) & (loans[\"loan_status\"] == 1)\n",
    "tp = len(predictions[tp_filter])\n",
    "\n",
    "# False negatives.\n",
    "fn_filter = (predictions == 0) & (loans[\"loan_status\"] == 1)\n",
    "fn = len(predictions[fn_filter])\n",
    "\n",
    "# True negatives\n",
    "tn_filter = (predictions == 0) & (loans[\"loan_status\"] == 0)\n",
    "tn = len(predictions[tn_filter])\n",
    "\n",
    "# Rates\n",
    "tpr = tp / float((tp + fn))\n",
    "fpr = fp / float((fp + tn))\n",
    "\n",
    "print(tpr)\n",
    "print(fpr)\n",
    "print predictions[:20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.731799521545\n",
      "0.478985635751\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.cross_validation import cross_val_predict\n",
    "penalty = {\n",
    "    0: 5,\n",
    "    1: 1\n",
    "}\n",
    "\n",
    "lr = LogisticRegression(class_weight=penalty)\n",
    "kf = KFold(features.shape[0], random_state=1)\n",
    "predictions = cross_val_predict(lr, features, target, cv=kf)\n",
    "predictions = pd.Series(predictions)\n",
    "\n",
    "# False positives.\n",
    "fp_filter = (predictions == 1) & (loans[\"loan_status\"] == 0)\n",
    "fp = len(predictions[fp_filter])\n",
    "\n",
    "# True positives.\n",
    "tp_filter = (predictions == 1) & (loans[\"loan_status\"] == 1)\n",
    "tp = len(predictions[tp_filter])\n",
    "\n",
    "# False negatives.\n",
    "fn_filter = (predictions == 0) & (loans[\"loan_status\"] == 1)\n",
    "fn = len(predictions[fn_filter])\n",
    "\n",
    "# True negatives\n",
    "tn_filter = (predictions == 0) & (loans[\"loan_status\"] == 0)\n",
    "tn = len(predictions[tn_filter])\n",
    "\n",
    "# Rates\n",
    "tpr = tp / float((tp + fn))\n",
    "fpr = fp / float((fp + tn))\n",
    "\n",
    "print(tpr)\n",
    "print(fpr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.973862193213\n",
      "0.940946976414\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.cross_validation import cross_val_predict\n",
    "rf = RandomForestClassifier(n_estimators=10,class_weight=\"balanced\", random_state=1)\n",
    "#print help(RandomForestClassifier)\n",
    "kf = KFold(features.shape[0], random_state=1)\n",
    "predictions = cross_val_predict(rf, features, target, cv=kf)\n",
    "predictions = pd.Series(predictions)\n",
    "\n",
    "# False positives.\n",
    "fp_filter = (predictions == 1) & (loans[\"loan_status\"] == 0)\n",
    "fp = len(predictions[fp_filter])\n",
    "\n",
    "# True positives.\n",
    "tp_filter = (predictions == 1) & (loans[\"loan_status\"] == 1)\n",
    "tp = len(predictions[tp_filter])\n",
    "\n",
    "# False negatives.\n",
    "fn_filter = (predictions == 0) & (loans[\"loan_status\"] == 1)\n",
    "fn = len(predictions[fn_filter])\n",
    "\n",
    "# True negatives\n",
    "tn_filter = (predictions == 0) & (loans[\"loan_status\"] == 0)\n",
    "tn = len(predictions[tn_filter])\n",
    "\n",
    "# Rates\n",
    "tpr = tp / float((tp + fn))\n",
    "fpr = fp / float((fp + tn))\n",
    "\n",
    "print(tpr)\n",
    "print(fpr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
